﻿//Copyright 2010 http://tv4home.codeplex.com
//This file is part of TV4Home.WebInterface.UI.
//TV4Home.WebInterface.UI is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
//TV4Home.WebInterface.UI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//You should have received a copy of the GNU General Public License along with TV4Home.WebInterface.UI If not, see http://www.gnu.org/licenses/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace TV4Home.WebInterface.UI.Code
{
    public static class TimeSpanExtensionMethods
    {
        public enum MaximumAccuracy
        {
            Day,
            Hour,
            Minute,
            Second
        }

        /// <summary>
        /// Formats the TimeSpan to a pretty string.
        /// </summary>
        /// <param name="ts">The timespan.</param>
        /// <param name="accuracy">The maximum accuracy the time span should be formatted to.</param>
        /// <returns>A string with the pretty formatted time span.</returns>
        public static string ToPrettyString(this TimeSpan ts, MaximumAccuracy accuracy = MaximumAccuracy.Minute)
        {
            StringBuilder sb = new StringBuilder();

            // build the formatted strings
            if (ts.Days != 0 && (accuracy == MaximumAccuracy.Day || accuracy == MaximumAccuracy.Hour || accuracy == MaximumAccuracy.Minute || accuracy == MaximumAccuracy.Second))
                sb.AppendFormat("{0} {1};",
                    ts.Days,
                    ts.Days == 1 ? Resources_TimeSpanExtensionMethods.Day_Singular : Resources_TimeSpanExtensionMethods.Day_Plural);

            if (ts.Hours != 0 && (accuracy == MaximumAccuracy.Hour || accuracy == MaximumAccuracy.Minute || accuracy == MaximumAccuracy.Second))
                sb.AppendFormat("{0} {1};",
                    ts.Hours,
                    ts.Hours == 1 ? Resources_TimeSpanExtensionMethods.Hour_Singular : Resources_TimeSpanExtensionMethods.Hour_Plural);

            if (ts.Minutes != 0 && (accuracy == MaximumAccuracy.Minute || accuracy == MaximumAccuracy.Second))
                sb.AppendFormat("{0} {1};",
                    ts.Minutes,
                    ts.Minutes == 1 ? Resources_TimeSpanExtensionMethods.Minute_Singular : Resources_TimeSpanExtensionMethods.Minute_Plural);

            if (ts.Seconds != 0 && (accuracy == MaximumAccuracy.Second))
                sb.AppendFormat("{0} {1};",
                    ts.Seconds,
                    ts.Seconds == 1 ? Resources_TimeSpanExtensionMethods.Second_Singular : Resources_TimeSpanExtensionMethods.Second_Plural);

            // remove the last seperator
            if (sb.Length > 0)
                sb.Remove(sb.Length - 1, 1);

            // get the index of the last seperator
            int lastIndexOfSeperator = sb.ToString().LastIndexOf(';');

            // if the result string contains any seperator characters, replace the last one with the "and" phrase
            if (lastIndexOfSeperator > 0)
                sb.Replace(";", " " + Resources_TimeSpanExtensionMethods.And + " ", lastIndexOfSeperator, 1);

            // replace all seperators by the correct seperator
            sb.Replace(";", Resources_TimeSpanExtensionMethods.Seperator + " ");

            return sb.ToString();
        }
    }
}